package io.renren.modules.bmx.service.impl;

import io.renren.common.utils.MapUtils;
import io.renren.common.validator.Assert;
import io.renren.modules.bmx.dao.MoniSecondtypeDao;
import io.renren.modules.bmx.dao.TemporaryDao;
import io.renren.modules.bmx.entity.TemporaryEntity;
import io.renren.modules.bmx.vo.MoniSecondtypeVo;
import io.renren.modules.bmx.vo.SubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.bmx.dao.SubjectDao;
import io.renren.modules.bmx.entity.SubjectEntity;
import io.renren.modules.bmx.service.SubjectService;

import javax.security.auth.Subject;


@Service("subjectService")
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectEntity> implements SubjectService {
    @Autowired(required = false)
    TemporaryDao temporaryDao;

    @Autowired(required = false)
    private MoniSecondtypeDao moniSecondtypeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubjectEntity> page = this.page(
                new Query<SubjectEntity>().getPage(params),
                new QueryWrapper<SubjectEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 正常练习
     * @param secondtypeIds
     * @param random
     * @param userId
     * @return
     */
    @Override
    public List<SubjectVo> getSubjectVoForTestExercise(List<Long> secondtypeIds, int random, Long userId) {
        List<SubjectVo> subjectVoList = baseMapper.selectSubjectBySeIds(secondtypeIds);
        if (random == 1) {
            Collections.shuffle(subjectVoList);
        }
        //开始做题前，清空该用户的temporary记录
        Integer result = temporaryDao.deleteByMap(new MapUtils().put("uid",userId));

        for (int i = 0; i < subjectVoList.size(); i++) {
            SubjectVo subjectVo = subjectVoList.get(i);
            subjectVo.setSort(i);
            TemporaryEntity temporaryEntity = new TemporaryEntity().setSsid(subjectVo.getId()).setUid(userId.intValue()).setTorder(i).setStatus(0).setFirst(0).setChoose(0);
            temporaryDao.insert(temporaryEntity);
            subjectVo.setTemporary(temporaryEntity);
        }
        return subjectVoList;
    }

    /**
     * 继续做题
     * @param userId
     * @return
     */

    @Override
    public List<SubjectVo> getSubjectVoForContinueTestExercise(Long userId) {
        List<SubjectVo> subjectVoList = baseMapper.selectTemporarySubjectByUserId(userId);
        return subjectVoList;
    }

    /**
     *   我的错题
     * @param userId
     * @return
     */
    @Override
    public List<SubjectVo> getSubjectVoForErrorTestExercise(Long userId) {
        return baseMapper.selectErrorSubjectByUserId(userId);
    }

    /**
     * 本次错题练习
     * @param userId
     * @return
     */

    @Override
    public List<SubjectVo> getSubjectVoForCurrentError(Long userId) {
        temporaryDao.delete(new QueryWrapper<TemporaryEntity>().ne("first",2).eq("uid",userId));
        List<TemporaryEntity> temporaryEntities = temporaryDao.selectByMap(new MapUtils().put("uid", userId));
        for (int i = 0; i < temporaryEntities.size(); i++) {
            TemporaryEntity temporaryEntity = temporaryEntities.get(i);
            temporaryEntity.setTorder(i).setFirst(0).setStatus(0).setChoose(0);
            temporaryDao.updateById(temporaryEntity);
        }
        return getSubjectVoForContinueTestExercise(userId);
    }

    /**
     * 本次错题练习
     * @param courseId
     * @return
     */

    @Override
    public List<SubjectVo> getSubjectVoForMoiTest(Long courseId,Long userId) {
        List<MoniSecondtypeVo> moniSecondtypeVos = moniSecondtypeDao.selelctVoByCourseId(courseId);
        List<SubjectVo> all = new ArrayList<>();
        for (MoniSecondtypeVo moniSecondtypeVo : moniSecondtypeVos) {
            List<SubjectVo> subjectList = moniSecondtypeVo.getSubjectVoList();
            Integer randomCount = moniSecondtypeVo.getRandomCount();
            Collections.shuffle(subjectList);
            List<SubjectVo> subjectEntities = randomCount > subjectList.size() ? subjectList : subjectList.subList(0, randomCount);
            all.addAll(subjectEntities);
        }
        Collections.shuffle(all);
        for (int i = 0; i < all.size(); i++) {
            SubjectVo subjectVo = all.get(i);
            subjectVo.setSort(i);
            TemporaryEntity temporaryEntity = new TemporaryEntity().setSsid(subjectVo.getId()).setUid(userId.intValue()).setTorder(i).setStatus(0).setFirst(0).setChoose(0);
            //temporaryDao.insert(temporaryEntity);
            subjectVo.setTemporary(temporaryEntity);
        }
        return all;
    }
}
